//  Inside Microsoft Exchange 2007 Web Services 
//  Chapter 4
//
//  Copyright (c) 2007 David Sterling, Ben Spain, Mark Taylor, Huw Upshall, Michael Mainer.  
//  All rights reserved.
//
using System;
using System.Collections.Generic;
using System.Text;
using ProxyHelpers.EWS;
using System.Net;

namespace CreateFolderUsingProxy
{
    class Program
    {
        static void Main(string[] args)
        {
            ExchangeServiceBinding binding = new ExchangeServiceBinding();
            binding.Url = @"https://yourServer/ews/Exchange.asmx";
            binding.Credentials = new NetworkCredential(
                "username",
                "password",
                "domain");

            CreateFolderUsingProxy(binding);
        }

        /// <summary>
        /// Creates a folder using proxy (Listing 4-6)
        /// </summary>
        /// <param name="binding">Binding to use for the call</param>
        /// 
        public static void CreateFolderUsingProxy(ExchangeServiceBinding binding)
        {
            // Create our request
            //
            CreateFolderType createRequest = new CreateFolderType();
            createRequest.ParentFolderId = new TargetFolderIdType();

            // Set the parent folder that we want to create our new folder in.
            //
            DistinguishedFolderIdType parentId = new DistinguishedFolderIdType();
            parentId.Id = DistinguishedFolderIdNameType.inbox;
            createRequest.ParentFolderId.Item = parentId;

            // Define the structure of our new folder.
            //
            FolderType folderToCreate = new FolderType();
            folderToCreate.DisplayName = "My First Proxy Folder";

            // Set the folder on our request
            //
            createRequest.Folders = new BaseFolderType[] { folderToCreate };

            // Make the call using the service binding instance
            //
            CreateFolderResponseType response = binding.CreateFolder(createRequest);

            // Since we created a single item, we can expect a single response message.  // The response message will be of type FolderInfoResponseMessageType.  We // must cast it appropriately.
            //
            FolderInfoResponseMessageType responseMessage =
                   response.ResponseMessages.Items[0] as FolderInfoResponseMessageType;

            if (responseMessage.ResponseCode == ResponseCodeType.NoError)
            {
                Console.WriteLine("New folder id is :" +
                          responseMessage.Folders[0].FolderId.Id);
            }
            else
            {
                Console.WriteLine("Hmmm.  CreateFolder failed.  Error code: " +
                        responseMessage.ResponseCode.ToString());
            }
        }
    }
}
